package com.tony.easymvc.core;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import com.google.common.io.Files;
import com.tony.easymvc.core.bean.ConfigModel;
import com.tony.easymvc.po.ProjectBiz;

public class POCopyUtil {

    public static void copy(ConfigModel config) throws IOException {
        String popath = config.getPoPath();
        System.out.println(popath);

        // ${return config.project.poPackage;}.${return biz.bizName}
        // ${return config.parentProjectPath;}/${return
        // config.project.pix;}_po/src/main/java

        List<ProjectBiz> bizs = config.getProject().getProjectBizs();
        for (int i = 0; i < bizs.size(); i++) {
            ProjectBiz biz = bizs.get(i);

            String path = config.getParentProjectPath() + "/" + config.getProject().getPix() + "_po/src/main/java";
            String poPackage = config.getProject().getPoPackage() + "." + biz.getBizName();
            String poPath = getPackagePath(poPackage);
            path += "/" + poPath;

            File tempDir = new File(path);
            String[] aa = tempDir.list();
            File[] poFiles = tempDir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith(".java") && !name.contains("Example.java");
                }
            });

            String apiPath = config.getParentProjectPath() + "/" + config.getProject().getPix() + "_api/src/main/java";
            String apiPackage = config.getProject().getScanPackage() + ".api." + biz.getBizName() + ".apiBean";
            String apiPackagePath = getPackagePath(apiPackage);
            apiPath += "/" + apiPackagePath;
            File apiDir = new File(apiPath);
            apiDir.mkdirs();

            for (int j = 0; j < poFiles.length; j++) {
                List<String> lines = Files.readLines(poFiles[j], Charset.forName(FileGenerator.CHARSET));
                File apiFile = new File(apiPath + "/" + getApiFileName(poFiles[j].getName()));
                System.out.println(lines.size());
                try (BufferedWriter writer = Files.newWriter(apiFile, Charset.forName(FileGenerator.CHARSET))) {
                    for (int k = 0; k < lines.size(); k++) {
                        String line = lines.get(k);
                        if (line.trim().startsWith("package ")) {
                            String packageName = "package " + apiPackage + ";";
                            writer.write(packageName);
                        } else if (line.trim().startsWith("public") && line.contains(" class ")) {
                            String[] arr = line.split("\\s+");
                            boolean isRp = false;
                            String temp = "";
                            for (int l = 0; l < arr.length; l++) {
                                String token = arr[l];
                                if (isRp) {
                                    temp = token;
                                    break;
                                }
                                if (token.equals("class")) {
                                    isRp = true;
                                }
                            }
                            writer.write(line.replace(temp, temp + "ApiBean"));
                        } else {
                            writer.write(line);
                        }
                        writer.newLine();
                    }
                    writer.flush();
                }
            }
        }
    }

    private static String getApiFileName(String filename) {
        return filename.replace(".", "ApiBean.");
    }

    private static String getPackagePath(String packageName) {
        return packageName.replaceAll("\\.", "/");
    }

}
